
/**
 * This class constructs the edges
 * @author Hyangjin Lee
 *
 */
public class Edge implements Comparable<Edge> {
	
	private int weight;
	private Vertex u, v;
	
	/**
	 * Constructs edges
	 * @param u The vertex on one side
	 * @param v The vertex on the other side of the edge
	 * @param weight The weight between two vertices
	 */
	public Edge(Vertex u, Vertex v, int weight) {
		this.u = u;
		this.v = v;
		this.weight = weight;
	}
	
	@Override
	public String toString() {
		
		return u + " and " + v + " are " + weight + " units apart.";
	}

	@Override
	public int compareTo(Edge that) {
		if(weight < that.getWeight())
			return -1;
		else if(weight == that.getWeight())
			return 0;
		else
			return 1;
	}
	
	/**
	 * Returns the weight of the edge
	 * @return The weight of the edge
	 */
	public int getWeight() {
		return weight;
	}

	/**
	 * Returns the vertex on one end of the edge
	 * @return The vertex on one end of the edge
	 */
	public Vertex getU() {
		return u;
	}

	/**
	 * Returns the vertex on the other end of the edge
	 * @return The vertex on the other end of the edge
	 */
	public Vertex getV() {
		return v;
	}

	@Override
	public boolean equals(Object o) {
		if (o != null && o instanceof Edge) {
			Edge e = (Edge) o;
			boolean w = weight == e.weight;
			if (u.equals(e.u) && v.equals(e.v)) {
				return w;
			} else if (u.equals(e.v) && v.equals(e.u)) {
				return w;
			} else {
				return false;
			}
		} else { 
			return false;
		}
	}
	
	@Override
	public int hashCode() {
		return u.hashCode() ^ v.hashCode() ^ weight;
	}
}
